/* 
操作方法
*/
const fs = require('fs')
const { url } = require('../../upload/index')
const { logger } = require('../../util/log')
const qr = require('qr-image')
const { status200, status500 } = require('../../util/status')

const fun = (req) => {
    /* 假设req.body.test==1 执行操作写入操作 */
    if (req.body.test == '1') {
        const fsURL = url + '/' + `${req.body.data.title}.txt`
        fs.writeFileSync(fsURL, req.body.data.content, (err) => {
            if (err) {
                logger.error(err)
            }
            logger.info('ok')
        })
    }
}


/* 
生成二维码
*/

const createCode = (res) => {
    console.log('生成二维码操作')
    const httpUrl = JSON.stringify('https://m.dongqiudi.com/video/')
    const bufferStr = Buffer.from(httpUrl).toString('base64')
    console.log(bufferStr, 'str111')
    var code = qr.image(bufferStr, { type: 'png' });
    const codeURL = `${url}/qrcode.png`
    var pic = code.pipe(fs.createWriteStream(codeURL));
    pic.on('error', function (err) {
        logger.error(err)
        status500(res,{msg:'error'})
    });
    pic.on('finish', function () {
        console.log('二维码生成完毕');
    });
    status200(res,{msg:'success',data:'http://127.0.0.1:9066/qrcode.png'})
}


module.exports = {
    fun,
    createCode
}